<template>
  <div>
    <h1>人员列表</h1>
    <h2 style="color: red">Count组件求和是:{{ sum }}</h2>
    <h3>列表第一个人员姓名:{{ firstPresonName }}</h3>
    <input type="text" placeholder="请添加" v-model="name" />
    <ul>
      <li v-for="p in person" :key="p.id">{{ p.name }}</li>
    </ul>
    <button @click="add">点击添加</button>
    <button @click="addPresonChen">添加一个姓陈的</button>
    <button @click="randmName">添加一个随机人员</button>
  </div>
</template>

<script>
// import {mapState} from 'vuex'
import { nanoid } from "nanoid";
export default {
  name: "Person",
  data() {
    return {
      name: "",
    };
  },
  computed: {
    person() {
      return this.$store.state.PersonAbout.person;
    },
    sum() {
      return this.$store.state.CountAbout.sum;
    },
    firstPresonName() {
      return this.$store.getters["PersonAbout/firstPresonName"]; //注意不用map选择命名空间对象下的方法需要/
    },
  },
  methods: {
    add() {
      const personObj = { id: nanoid(), name: this.name };
      this.$store.commit("PersonAbout/ADD_PERSON", personObj); //注意不用map选择命名空间对象下的方法需要/
      this.name = "";
    },
    addPresonChen() {
      const personObj = { id: nanoid(), name: this.name };
      this.$store.dispatch("PersonAbout/addPresonChen", personObj); //注意不用map选择命名空间对象下的方法需要/
      this.name = "";
    },
    randmName() {
      return this.$store.dispatch("PersonAbout/randmName");
    },
  },
  mounted() {
    // console.log(this.$store.getters);
  },
};
</script>

<style>
</style>